package com.fr.similarity;

import java.util.List;

/**
 * @author furong
 * @version 创建时间：2016-3-31 上午11:41:56
 */
public class DefaultSimilarity implements Similarity{
	private final int cityWeight = 40,
			industryWeight = 3,
			developWeight = 3,
			scaleWeight = 2,
			styleWeight = 2,
			salaryWeight = 10;

	@Override
	public double calculate(A a, B b) {
		List<Long> city = a.getCity();
		List<Long> industry = a.getIndustry();
		List<Long> develop = a.getDevelop();
		List<Long> scale = a.getScale();
		List<Long> style = a.getStyle();
		double min = a.getMinSalary();
		double max = a.getMaxSalary();
		List<String> salary = AnnualSalaryCaculator.compartPrice(min, max);
		
		double score = 0;
		score +=  match(city, b.getCity(), cityWeight);
		score +=  match(industry, b.getIndustry(), industryWeight);
		score +=  match(develop, b.getDevelop(), developWeight);
		score +=  match(scale, b.getScale(), scaleWeight);
		score +=  match(style, b.getStyle(), styleWeight);
		score +=  matchSalary(salary, b.getSalaryRange(), salaryWeight);
		return score;
	}

	private double matchSalary(List<String> salary, List<StringCount> list,
			int weight) {
		if (salary == null || salary.size() == 0){
			return 0;
		}
		int value = 0;
		for(String id : salary){
			value += match(id, list, weight); 
		}
		int total = getTotal(list);
		if (total > 0){
			return 1.0D * value * weight /total;
		}
		return 0;
	}

	private double match(List<Long> ids, List<StringCount> list, int weight) {
		if (ids == null || ids.size() == 0){
			return 0;
		}
		int value = 0;
		for(long id : ids){
			value += match(String.valueOf(id), list, weight); 
		}
		int total = getTotal(list);
		if (total > 0){
			return 1.0D * value * weight /total;
		}
		return 0;
	}

	private int getTotal(List<StringCount> list) {
		int total = 0;
		for(StringCount sc : list){
			total += sc.getValue();
		}
		return total;
	}

	private int match(String id, List<StringCount> list, int weight) {
		for(StringCount sc : list){
			if (id.equalsIgnoreCase(sc.getKey())){
				return sc.getValue();
			}
		}
		return 0;
	}

}
